<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="第三方编号" prop="supplierCode">
        <el-input v-model="formData.supplierCode" placeholder="请输入第三方编号" />
      </el-form-item>
      <el-form-item label="第三方名称" prop="supplierName">
        <el-input v-model="formData.supplierName" placeholder="请输入第三方名称" />
      </el-form-item>
      <el-form-item label="简称" prop="abbreviation">
        <el-input v-model="formData.abbreviation" placeholder="请输入简称" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input v-model="formData.contactPerson" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="contactPersonPhone">
        <el-input v-model="formData.contactPersonPhone" placeholder="请输入联系人手机号" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="mail">
        <el-input v-model="formData.mail" placeholder="请输入电子邮箱" />
      </el-form-item>
      <el-form-item label="主体性质" prop="mainNature">
        <el-input v-model="formData.mainNature" placeholder="请输入主体性质" />
      </el-form-item>
      <el-form-item label="第三方类型" prop="supplierType">
        <el-select v-model="formData.supplierType" placeholder="请选择第三方类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业类型" prop="industryType">
        <el-select v-model="formData.industryType" placeholder="请选择行业类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="物流属性" prop="logisticsAttr">
        <el-input v-model="formData.logisticsAttr" placeholder="请输入物流属性" />
      </el-form-item>
      <el-form-item label="纳税人类型" prop="taxpayerType">
        <el-select v-model="formData.taxpayerType" placeholder="请选择纳税人类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="纳税人识别号" prop="taxpayerCode">
        <el-input v-model="formData.taxpayerCode" placeholder="请输入纳税人识别号" />
      </el-form-item>
      <el-form-item label="开户行名称" prop="accountBankName">
        <el-input v-model="formData.accountBankName" placeholder="请输入开户行名称" />
      </el-form-item>
      <el-form-item label="开户行账号" prop="accountCode">
        <el-input v-model="formData.accountCode" placeholder="请输入开户行账号" />
      </el-form-item>
      <el-form-item label="所在省份" prop="province">
        <el-input v-model="formData.province" placeholder="请输入所在省份" />
      </el-form-item>
      <el-form-item label="所在城市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入所在城市" />
      </el-form-item>
      <el-form-item label="所在区域" prop="region">
        <el-input v-model="formData.region" placeholder="请输入所在区域" />
      </el-form-item>
      <el-form-item label="服务区域" prop="serviceArea">
        <el-input v-model="formData.serviceArea" placeholder="请输入服务区域" />
      </el-form-item>
      <el-form-item label="服务品类" prop="serviceCategory">
        <el-input v-model="formData.serviceCategory" placeholder="请输入服务品类" />
      </el-form-item>
      <el-form-item label="启停状态" prop="disabled">
        <el-radio-group v-model="formData.disabled">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { SupplierApi, SupplierVO } from '@/api/mdm/supplier'

/** 第三方信息 表单 */
defineOptions({ name: 'SupplierForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  supplierCode: undefined,
  supplierName: undefined,
  abbreviation: undefined,
  contactPerson: undefined,
  contactPersonPhone: undefined,
  mail: undefined,
  mainNature: undefined,
  supplierType: undefined,
  industryType: undefined,
  logisticsAttr: undefined,
  taxpayerType: undefined,
  taxpayerCode: undefined,
  accountBankName: undefined,
  accountCode: undefined,
  province: undefined,
  city: undefined,
  region: undefined,
  serviceArea: undefined,
  serviceCategory: undefined,
  disabled: undefined,
})
const formRules = reactive({
  supplierCode: [{ required: true, message: '第三方编号不能为空', trigger: 'blur' }],
  supplierName: [{ required: true, message: '第三方名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SupplierApi.getSupplier(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SupplierVO
    if (formType.value === 'create') {
      await SupplierApi.createSupplier(data)
      message.success(t('common.createSuccess'))
    } else {
      await SupplierApi.updateSupplier(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    supplierCode: undefined,
    supplierName: undefined,
    abbreviation: undefined,
    contactPerson: undefined,
    contactPersonPhone: undefined,
    mail: undefined,
    mainNature: undefined,
    supplierType: undefined,
    industryType: undefined,
    logisticsAttr: undefined,
    taxpayerType: undefined,
    taxpayerCode: undefined,
    accountBankName: undefined,
    accountCode: undefined,
    province: undefined,
    city: undefined,
    region: undefined,
    serviceArea: undefined,
    serviceCategory: undefined,
    disabled: undefined,
  }
  formRef.value?.resetFields()
}
</script>